﻿using FastCloud.Base;
using FastCloud.ORG.DB;
using FastCloud.ORG.Entity;
using FastIOC.Attribute;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.ORG
{
    [Route("frame/org/role")]
    public class RoleController:BaseController
    {
        [AutoWired]
        private DB_Role DB_Role;
        [AutoWired]
        private DB_ModuleRight DB_ModuleRight;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/ORG/Role/Role_List.cshtml");
        }

        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/ORG/Role/Role_Add.cshtml");
        }

        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/ORG/Role/Role_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string RoleName = ValueConvert.ConvertToString(GetFormParam("RoleName"));
            string GroupGuid = ValueConvert.ConvertToString(GetFormParam("GroupGuid"));
            return Result.OK(DB_Role.SelectPage(RoleName, GroupGuid));
        }

        [Route("oplistall"), HttpPost]
        public IActionResult OpListAll()
        {
            return Result.OK(DB_Role.SelectAll());
        }

        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_Role Entity)
        {
            if (DB_Role.ExistRole(Entity.RoleName))
            {
                return Result.Error("已存在相同名称的角色！");
            }
            else
            {
                DB_Role.Insert(Entity);
                return Result.OK();
            }
        }

        [Route("opupdate"), HttpPost]
        public IActionResult OpUpdate(Frame_Role Entity)
        {
            if (DB_Role.ExistRole(Entity.GroupGuid, Entity.RoleName))
            {
                return Result.Error("已存在相同名称的角色！");
            }
            else
            {
                DB_Role.UpdateByID(Entity);
                return Result.OK();
            }
        }

        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string RoleGuid = ValueConvert.ConvertToString(GetFormParam("RoleGuid"));
            return Result.OK(DB_Role.SelectOne(RoleGuid));
        }

        [Route("opbatchdelete"), HttpPost]
        [Transitional(TransitonLevel = IsolationLevel.ReadUncommitted)]
        public virtual IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            List<string> KeyIDList = KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            DB_Role.DeleteBatch(KeyIDList);
            //删除模块权限
            foreach (string RoleGuid in KeyIDList)
            {
                DB_ModuleRight.DeleteByFiled("RightGuid", RoleGuid);
            }
            return Result.OK();
        }
    }
}
